﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;

namespace Backuper
{

    class EmailService
    {
        public enum EmailServiceName
        {
            Hotmail,
            Gmail,
            MaxiMedia
        };

        private EmailServiceName serviceType;
        private string hostAddress;
        private int hostPort;
        private string username, password;
        private MailMessage message;

        public EmailService(EmailServiceName type,string Username,string Password){
            updateMailService(type,Username,Password);
        }

        public void updateMailService(EmailServiceName type, string Username,string Password){
            serviceType = type;
            username = Username;
            password = Password;
            hostAddress = getServiceAddress();
            hostPort = getServicePort();
        }

        private string getServiceAddress()
        {
            string Address=null;
            switch (serviceType)
            {
                case EmailServiceName.Hotmail:
                    Address="smtp.live.com";
                    break;
                case EmailServiceName.Gmail:
                    Address = "smtp.gmail.com";
                    break;
                case EmailServiceName.MaxiMedia:
                    Address = "";
                    break;
            }
            return Address;
        }

        private int getServicePort()
        {
            int port=0;
            switch (serviceType)
            {
                case EmailServiceName.Hotmail:
                    port = 25;
                    break;
                case EmailServiceName.Gmail:
                    port = 587;
                    break;
                case EmailServiceName.MaxiMedia:
                    port = 0;
                    break;
            }
            return port;
        }

        private string getServiceImage()
        {
            string image = null;
            switch (serviceType)
            {
                case EmailServiceName.Hotmail:
                    image = ".\\hotmail.jpg";
                    break;
                case EmailServiceName.Gmail:
                    image = ".\\gmail.png";
                    break;
                case EmailServiceName.MaxiMedia:
                    image = "";
                    break;
            }
            return image; 
        }

        public bool sendMessage(MailMessage mymessage)
        {
            try
            {
                message = mymessage;
                SmtpClient Client = new SmtpClient(hostAddress, hostPort);
                Client.EnableSsl = true;
                Client.Credentials = new System.Net.NetworkCredential(username, password);
                // Client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                //Client.SendAsync(message, null); 
                Client.Send(message);
            }
            catch (Exception e) { return false; }
            return true;
        }

       /* private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            Console.WriteLine("Done!");
        }*/
    }
}
